package com.study.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.study.app.domain.vo.AppUserVo;
import com.study.app.mapper.AppUserMapper;
import com.study.common.utils.StringUtils;
import com.study.common.core.page.TableDataInfo;
import com.study.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.study.app.domain.bo.AppUserInfoSetBo;
import com.study.app.domain.vo.AppUserInfoSetVo;
import com.study.app.domain.AppUserInfoSet;
import com.study.app.mapper.AppUserInfoSetMapper;
import com.study.app.service.IAppUserInfoSetService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户消息设置Service业务层处理
 *
 * @author study
 * @date 2024-04-09
 */
@RequiredArgsConstructor
@Service
public class AppUserInfoSetServiceImpl implements IAppUserInfoSetService {

    private final AppUserInfoSetMapper baseMapper;
    private final AppUserMapper userMapper;

    /**
     * 查询用户消息设置
     */
    @Override
    public AppUserInfoSetVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public AppUserInfoSetVo queryByUserId(Long userId) {
        AppUserVo appUserVo = userMapper.selectVoById(userId);
        AppUserInfoSetBo appUserInfoSetBo = new AppUserInfoSetBo();
        appUserInfoSetBo.setUserCode(appUserVo.getUserCode());
        LambdaQueryWrapper<AppUserInfoSet> lqw = buildQueryWrapper(appUserInfoSetBo);
        AppUserInfoSet appUserInfoSet = baseMapper.selectVoOne(lqw, AppUserInfoSet.class);
//        if (appUserInfoSet != null) {
//            if (appUserInfoSet.getRenewalNotice() == null || appUserInfoSet.getExpirationReminder() == null || appUserInfoSet.getVersionUpdate() == null) {
//                appUserInfoSet.setRenewalNotice(0);
//                appUserInfoSet.setExpirationReminder(0);
//                appUserInfoSet.setVersionUpdate(0);
//            }
//            if (appUserInfoSet.getElectricFence() == null || appUserInfoSet.getIllegalMovement() == null || appUserInfoSet.getTheftAlarm() == null){
//                appUserInfoSet.setElectricFence(0);
//                appUserInfoSet.setIllegalMovement(0);
//                appUserInfoSet.setTheftAlarm(0);
//            }
//        }

        return  BeanUtil.toBean(appUserInfoSet, AppUserInfoSetVo.class);
    }

    /**
     * 查询用户消息设置列表
     */
    @Override
    public TableDataInfo<AppUserInfoSetVo> queryPageList(AppUserInfoSetBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUserInfoSet> lqw = buildQueryWrapper(bo);
        Page<AppUserInfoSetVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户消息设置列表
     */
    @Override
    public List<AppUserInfoSetVo> queryList(AppUserInfoSetBo bo) {
        LambdaQueryWrapper<AppUserInfoSet> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUserInfoSet> buildQueryWrapper(AppUserInfoSetBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUserInfoSet> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserCode()), AppUserInfoSet::getUserCode, bo.getUserCode());
        lqw.eq(bo.getElectricFence() != null, AppUserInfoSet::getElectricFence, bo.getElectricFence());
        lqw.eq(bo.getIllegalMovement() != null, AppUserInfoSet::getIllegalMovement, bo.getIllegalMovement());
        lqw.eq(bo.getTheftAlarm() != null, AppUserInfoSet::getTheftAlarm, bo.getTheftAlarm());
        lqw.eq(bo.getRenewalNotice() != null, AppUserInfoSet::getRenewalNotice, bo.getRenewalNotice());
        lqw.eq(bo.getExpirationReminder() != null, AppUserInfoSet::getExpirationReminder, bo.getExpirationReminder());
        lqw.eq(bo.getVersionUpdate() != null, AppUserInfoSet::getVersionUpdate, bo.getVersionUpdate());
        return lqw;
    }

    /**
     * 新增用户消息设置
     */
    @Override
    public Boolean insertByBo(AppUserInfoSetBo bo) {
        AppUserInfoSet add = BeanUtil.toBean(bo, AppUserInfoSet.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户消息设置
     */
    @Override
    public Boolean updateByBo(AppUserInfoSetBo bo) {
        AppUserInfoSet update = BeanUtil.toBean(bo, AppUserInfoSet.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUserInfoSet entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户消息设置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
